﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DocxToTextDemo;
using System.Diagnostics;

namespace BAASearchLib
{
    public enum DatePeriod { Before, Exact, After };
    public enum DateType { None, Created, Modified };

    internal abstract class FileComparer
    {
        public abstract bool Compare(FileInfo file);
    }

    internal class CompareFileName : FileComparer
    {
        private string fileName;

        public CompareFileName(string fileName)
        {
            this.fileName = fileName;
        }

        public override bool Compare(FileInfo file)
        {
            return file.Name.ToUpper().Contains(fileName.ToUpper());
        }
    }

    internal class CompareAuthor : FileComparer
    {
        private string author;

        public CompareAuthor(string author)
        {
            this.author = author;
        }

        public override bool Compare(FileInfo file)
        {
            if (new CompareFileType("docx").Compare(file))
            {
                DocxToText dtt = new DocxToText(file.FullName);
                string text = dtt.GetAuthor();
                return text.Equals(author);
            }
            if (new CompareFileType("pptx").Compare(file))
            {
                PptxToText dtt = new PptxToText(file.FullName);
                string text = dtt.GetAuthor();
                return text.Equals(author);
            }
            if (new CompareFileType("doc").Compare(file))
            {
                DocToText dtt = new DocToText(file.FullName);
                string text = dtt.GetAuthor();
                // should be called when all the doc files are checked
                // as it is more efficient to close this only ones, not recreate it 
                // for each doc file....but we can have leave it for now
                DocToTextWordApp.Close();
                return text.Equals(author);
            }
            if (new CompareFileType("ppt").Compare(file))
            {
                PptToText dtt = new PptToText(file.FullName);
                string text = dtt.GetAuthor();
                // should be called when all the ppt files are checked
                // as it is more efficient to close this only ones, not recreate it 
                // for each ppt file....but we can have leave it for now
                PptToTextPresentationApp.Close();
                return text.Equals(author);
            }
            if (new CompareFileType("txt").Compare(file))
            {
                TxtToText dtt = new TxtToText(file.FullName);
                string text = dtt.GetAuthor();
                return text.Equals(author);
            }
            else // more fileTypes, like; doc, ppt, pptx and later pdf
                return false;
        }
    }

    internal class CompareDateCreated : FileComparer
    {
        private DateTime dateTime;
        private DatePeriod datePeriod;

        public CompareDateCreated(DateTime dateTime)
        {
            this.dateTime = dateTime;
            this.datePeriod = DatePeriod.Exact;
        }

        public CompareDateCreated(DateTime dateTime, DatePeriod datePeriod)
            : this(dateTime)
        {
            this.datePeriod = datePeriod;
        }

        public override bool Compare(FileInfo file)
        {
            switch (datePeriod)
            {
                case DatePeriod.Before:
                    return dateTime.Date > file.CreationTime.Date;
                case DatePeriod.After:
                    return dateTime.Date < file.CreationTime.Date;
                default:
                    return dateTime.Date.Equals(file.CreationTime.Date);
            }
        }
    }

    internal class CompareDateModified : FileComparer
    {
        private DateTime dateTime;
        private DatePeriod datePeriod;

        public CompareDateModified(DateTime dateTime)
        {
            this.dateTime = dateTime;
            this.datePeriod = DatePeriod.Exact;
        }

        public CompareDateModified(DateTime dateTime, DatePeriod datePeriod)
            : this(dateTime)
        {
            this.datePeriod = datePeriod;
        }

        public override bool Compare(FileInfo file)
        {
            switch (datePeriod)
            {
                case DatePeriod.Before:
                    return dateTime.Date > file.LastWriteTime.Date;
                case DatePeriod.After:
                    return dateTime.Date < file.LastWriteTime.Date;
                default:
                    return dateTime.Date.Equals(file.LastWriteTime.Date);
            }
        }
    }

    internal class CompareDateCreatedSpan : FileComparer
    {
        private DateTime startDate;
        private DateTime endDate;

        public CompareDateCreatedSpan(DateTime startDate, DateTime endDate)
        {
            this.startDate = startDate;
            this.endDate = endDate;
        }

        public override bool Compare(FileInfo file)
        {
            return (file.CreationTime.Date >= startDate.Date) && (file.CreationTime.Date <= endDate.Date);
        }
    }

    internal class CompareDateModifiedSpan : FileComparer
    {
        private DateTime startDate;
        private DateTime endDate;

        public CompareDateModifiedSpan(DateTime startDate, DateTime endDate)
        {
            this.startDate = startDate;
            this.endDate = endDate;
        }

        public override bool Compare(FileInfo file)
        {
            return (file.LastWriteTime.Date >= startDate.Date) && (file.LastWriteTime.Date <= endDate.Date);
        }
    }

    internal class CompareFileType : FileComparer
    {
        private string fileType;

        public CompareFileType(string fileType)
        {
            this.fileType = fileType;
        }

        public override bool Compare(FileInfo file)
        {
            return (file.Extension.ToUpper() == "." + fileType.ToUpper());
        }
    }

    internal class CompareContents : FileComparer
    {
        private string lookfor;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lookfor">The string what we are looking for</param>
        public CompareContents(string lookfor)
        {
            this.lookfor = lookfor;
        }

        public override bool Compare(FileInfo file)
        {
            if (new CompareFileType("docx").Compare(file))
            {
                DocxToText dtt = new DocxToText(file.FullName);
                string text = dtt.ExtractText();
                return text.Contains(lookfor);
            }
            if (new CompareFileType("pptx").Compare(file))
            {
                PptxToText dtt = new PptxToText(file.FullName);
                string text = dtt.ExtractText();
                return text.Contains(lookfor);
            }
            if (new CompareFileType("doc").Compare(file))
            {
                DocToText dtt = new DocToText(file.FullName);
                string text = dtt.ExtractText();
                // should be called when all the doc files are checked
                // as it is more efficient to close this only ones, not recreate it 
                // for each doc file....but we can have leave it for now
                DocToTextWordApp.Close();
                return text.Contains(lookfor);
            }
            if (new CompareFileType("ppt").Compare(file))
            {
                PptToText dtt = new PptToText(file.FullName);
                string text = dtt.ExtractText();
                // should be called when all the ppt files are checked
                // as it is more efficient to close this only ones, not recreate it 
                // for each ppt file....but we can have leave it for now
                PptToTextPresentationApp.Close();
                return text.Contains(lookfor);
            }
            if (new CompareFileType("txt").Compare(file))
            {
                TxtToText dtt = new TxtToText(file.FullName);
                string text = dtt.ExtractText();
                return text.Contains(lookfor);
            }
            else // more fileTypes, like; doc, ppt, pptx and later pdf
                return false;
        }
    }
}
